layui.use(['form','laydate'], function(){
	var form = layui.form, laydate = layui.laydate, upload = layui.upload, table = layui.table;
	//日期选择框
	laydate.render({
		elem: '#birthday'
	});
	
	laydate.render({
		elem: '#entrytime'
	});
	
	/*加载学校类型下拉框*/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].id + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#selschooltype").append(str);
				form.render("select");
			}
		}
	});
	
	//加载阶段下拉框
	loadstage("stage", form);
	
	//根据阶段加载学校，年级下拉框
	form.on('select(stage)', function (data) {
        //学校下拉框
        loadSchool(data.value, "school", form);
        
    });
	
	/*点击学校类型下拉框*/
	form.on('select(selschooltype)', function(data) {
		var schooltype = data.value;
		if(schooltype=="00" || schooltype==null || schooltype=='null'){
			schooltype="";
		}
		$("#selschool").html("");
		//根据阶段筛选学校
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype='+schooltype,
			datatype : 'json',
			success : function(schooldata) {
				if (schooldata.code == 0) {
					var str = "<option value='00'>请选择学校</option>";
					for(var i = 0; i < schooldata.count; i++){
						str += '<option value=' + schooldata.data[i].schoolid + '>'+ schooldata.data[i].schoolName +'</option>';
					}
					$("#selschool").append(str);
					form.render("select");
				}
				else{
					layer.msg('暂无对应的学校', {icon: 2});
					form.render("select");
				}
			}
		});
	});
	
	/*选择学校后加载角色下拉框*/
	form.on('select(selschool)', function(data) {
		var schoolid = data.value;
		if(schoolid==00 || schoolid==null || schoolid=='null'){
			layer.msg('请选择所属学校！', {icon: 7});
		}
		$("#userType").html("");
		
		$.ajax({
			type : 'get',
			url : '../sysunifiedmanagement/getteacherrolelistbyschool?schoolid='+schoolid,
			datatype : 'json',
			success : function(roledata) {
				if (roledata.code == 0) {
					var str = "<option value='00'>请选择角色</option>";
					for(var i = 0; i < roledata.count; i++){
						str += '<option value=' + roledata.data[i].id + '>'+ roledata.data[i].name +'</option>';
					}
					$("#userType").append(str);
					form.render();
				}
				else{
					layer.msg(roledata.msg,{icon:2});
				}
			}
		});
	});

	$("#downloadteacher").click(function() {
		var schoolid = $("#school").val();
		if(schoolid == null || schoolid == "" || schoolid == "0"){
			layer.alert('请先选择学校！', {icon: 2});
			return;
		}
		else{
			$.ajax({
				type: 'get',
				url: '../systeachermanagement/downloadteacher?schoolid=' + schoolid,
				datatype: 'json',
				success: function(data) {
					if(data.code == 10001) {
						var link = document.createElement("a");
						link.href = data.path;
						link.style = "visibility:hidden";
						link.download = "教师信息表模板";
						document.body.appendChild(link);
						link.click();
						document.body.removeChild(link);										
					} else {
						layer.alert(data.msg, {icon: 2});
					}
				},
				error: function() {
					layer.alert('下载失败！', {icon: 2});
				}
			});
		}				
	});
	
	$("#uploadbtn").click(function(){
		var schoolid = $("#school").val();
		if(schoolid == null || schoolid == "" || schoolid == "0"){
			layer.alert('请先选择学校！', {icon: 2});
			return;
		}
    	$("#test4").click();
    });
	
    $("#test4").click(function(){
    	var schoolid = $("#school").val();
    	//指定允许上传的文件类型
		upload.render({ //允许上传的文件后缀
			elem : '#test4',
			url : '../systeachermanagement/getexceltablelist?schoolid=' + schoolid,
			accept : 'file', //普通文件
			exts : 'xls|xlsx|xlsm', //只允许上传压缩文件
			acceptMime : 'xls|xlsx|xlsm',
			done : function(res) {
				if (res.code == 0) {
					table.render({
						elem : '#teacher',
						url : '../systeachermanagement/excelloadtable?op=0',
						title : '教师数据表',
						skin : 'line',
						even : true,
						cols : [ [{
							type : 'numbers',
							title : '编号'
						}, {
							field : 'realName',
							title : '真实姓名'
						}, {
							field : 'sex',
							title : '性别'
						}, {
							field : 'email',
							title : '邮箱'
						}, {
							field : 'phone',
							title : '联系电话'
						}, {
							field : 'birthday',
							title : '生日'
						}, {
							field : 'entryTime',
							title : '入职时间',
						}] ]
					});
				} else {

				}
			}
		});
    });
    
	$("#saveTeaList").click(function() {
		$.ajax({
			type : 'get',
			url : '../systeachermanagement/savetealist',
			datatype : 'json',
			success : function(data) {
				if (data.code == 0) {
					layer.alert(data.msg, {icon: 1});							
				} else {
					layer.alert("导入失败，表格已替换为失败信息列表！", {icon: 2});
					table.render({
						elem : '#teacher',
						url : '../systeachermanagement/excelloadtable?op=1',
						title : '教师数据表',
						skin : 'line',
						even : true,
						cols : [ [{
							type : 'numbers',
							title : '编号'
						}, {
							field : 'realName',
							title : '真实姓名'
						}, {
							field : 'sex',
							title : '性别'
						}, {
							field : 'email',
							title : '邮箱'
						}, {
							field : 'phone',
							title : '联系电话'
						}, {
							field : 'birthday',
							title : '生日'
						}, {
							field : 'entryTime',
							title : '入职时间',
						}] ]
					});
				}
			},
			error : function() {
			}
		});

	});
	
	//添加一个教师
	form.on('submit(addTeacher)', function(teadata){
		var index = layer.load(0, {
			shade: 0.1
		});
		var schoolid=$("#selschool").val();
		var realname=$("#realname").val();
		var sex=$("input[name='sex']:checked").val()
		var birthday=$("#birthday").val();
		var email=$("#email").val();
		var phone=$("#phone").val();
		var tell=$("#tell").val();
		var worktel=$("#worktel").val();
		var address=$("#address").val();
		var post=$("#post").val();
		var graduateschool=$("#graduateschool").val();
		var entrytime=$("#entrytime").val();
		var roleid=$("#userType").val();
		
		if(schoolid==00 || schoolid==null || schoolid=="" || schoolid=='null'){
			layer.close(index);
			layer.msg("请选择所属学校！"); 
			return false;
		}
		if(roleid==00 || roleid==null || roleid=="" || roleid=='null'){
			layer.close(index);
			layer.msg("请选择教师角色！"); 
			return false;
		}
		$.ajax({
			url: '../systeachermanagement/addteacher',  
	        type: 'get', 
	        dataType: 'json',  
	        data:{realName:realname,sex:sex,birthday:birthday,
	        	Email:email,phone:phone,tell:tell,workTel:worktel,
	        	address:address,post:post,graduateSchool:graduateschool,
	        	entryTime:entrytime,roleid:roleid,schoolid:schoolid},
	        success: function (getdata) {
	          if (getdata.code == 10001) {
	        	  layer.close(index);
	        	  layer.confirm('教师信息添加成功！', {
					  btn: ['确定']
				  }, function(){
					  window.location.href = "../teacherManagement/editTeacher.jsp";
					  $("#liformtitle").text("教师信息查看");
				  });
	          }else { 
	        	  layer.close(index);
	        	  layer.alert("教师添加失败！",2);
	        	  return false;
	          }  
	        },
	        error:function(){
	        	layer.close(index);
	        	layer.alert("连接失败，请重试！",2); 
	        }
		});
	    
	});
});